package com.training.java.module2;

import java.util.Scanner;
import java.util.StringTokenizer;

public class QuestionWithAnswerVariants extends Question {

    private int correctAnswer;

    private String answersVariants;

    public QuestionWithAnswerVariants(String unparsedString) {
        fillQuestionInformation(unparsedString);
    }

    public void fillQuestionInformation(String unparsedString) {
        StringTokenizer stringTokenizer = new StringTokenizer(unparsedString, "|");
        stringTokenizer.nextToken();
        setQuestion(stringTokenizer.nextToken());
        answersVariants = stringTokenizer.nextToken();
        correctAnswer = convertStringToInt(stringTokenizer.nextToken());
    }

    private int convertStringToInt(String data) {
        return Integer.parseInt(data);
    }

    private int readCustomerAnswer() {

        Scanner inputScanner = new Scanner( System.in );

        System.out.println("Введите ваш ответ:");

        int customerAnswer;

        while(true){

            if(inputScanner.hasNextInt()){
                customerAnswer = inputScanner.nextInt();
                break;
            } else {
                System.out.println("Неверный формат ввода, попробуйте еще раз");
                inputScanner.next();
            }
        }
        return customerAnswer;
    }

    public boolean isCorrectAnswer() {
        int customerAnswer = readCustomerAnswer();
        if(customerAnswer ==correctAnswer) {
            return true;
        }
        return false;
    }

    public String  toString() {
        return String.format("%s%n%s", getQuestion(), answersVariants);
    }
}
